//
//  CZWelcomeViewController.swift
//  Swift -- 微博
//
//  Created by sks on 16/6/27.
//  Copyright © 2016年 mohaha. All rights reserved.
//

import UIKit
import SDWebImage

class CZWelcomeViewController: UIViewController {
    
    override func loadView() {
        view = bgImageView
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()

    }
    
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        
        self.headImageView.snp_updateConstraints { (make) in
            make.top.equalTo(view).offset(100)
            
        }
        
        
        UIView.animateWithDuration(3, delay: 1, usingSpringWithDamping: 0.6, initialSpringVelocity: 0, options: [], animations: {
            
               self.view.layoutIfNeeded()
            
            }) { (_) in
                
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.welcomeLbael.alpha = 1
                    }, completion: { (_) -> Void in
                        
                    
                //发送通知 告诉appDelegate 切换控制器
              NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVcNoti, object: "welcome")
                
                })
                
        }
    }

    
    //设置UI
    func setupUI() {
        
        //添加控件
        view.addSubview(headImageView)
        view.addSubview(welcomeLbael)
        headImageView.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 90, height: 90))
            make.top.equalTo(view).offset(400)
            make.centerX.equalTo(view)
        }
        welcomeLbael.snp_makeConstraints { (make) in
            make.centerX.equalTo(view)
            make.top.equalTo(headImageView.snp_bottom).offset(16)
        }
        
    }
    
    //懒加载背景图片
    private lazy var bgImageView :UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //头像
    private lazy var headImageView :UIImageView = {
    
        let imgView = UIImageView(image: UIImage(named: "avatar_default_big"))
        imgView.layer.cornerRadius = 45
        imgView.layer.masksToBounds = true
        imgView.layer.borderWidth = 3
        imgView.layer.borderColor = UIColor.orangeColor().CGColor
        
        //设置图片
        imgView.sd_setImageWithURL(NSURL(string:CZUserAccountTools.sharedTools.userAccount?.avater_large ?? ""), placeholderImage: UIImage(named: "avatar_default_big"))
        
     return imgView
    }()
    
    //欢迎回来laebl
    private lazy var welcomeLbael :UILabel = {
    
        let label = UILabel()
        label.text = "欢迎回来"
        label.font = UIFont.systemFontOfSize(20)
        label.textColor = UIColor.darkGrayColor()
        label.textAlignment = .Center
        label.alpha = 0
        return label
    }()
}
